package fr.iut2.tc1.projet.client;

import java.util.ArrayList;

import org.eclipse.swt.widgets.MessageBox;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.DialogBox;
import com.google.gwt.user.client.ui.FileUpload;
import com.google.gwt.user.client.ui.FormPanel;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.ListBox;
import com.google.gwt.user.client.ui.PopupPanel;
import com.google.gwt.user.client.ui.PushButton;
import com.google.gwt.user.client.ui.RadioButton;
import com.google.gwt.user.client.ui.TextArea;
import com.google.gwt.user.client.ui.SimplePanel;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.FormPanel.SubmitCompleteEvent;
import com.google.gwt.user.client.ui.FormPanel.SubmitCompleteHandler;
/**HomeAction permet de définir les actions qui seront 
 * réalisable a partir de l'onglet home*/

public class HomeAction extends Composite {
	private static ImageBund monBundle;
	private PushButton nouveauSponsor;
	private PushButton delSponsor;
	private VerticalPanel action;
	private SimplePanel vide;
	private String text;
	private Image icon;
	private Label ben;
	private Label tib;
	private final ServicesAsync services = GWT.create(Services.class);
	/**Dans le consctructeur de HomeAction on récupére l'image bundle et on creer
	 * Les boutons et informations necessaire a l'ajout et a la suppresion des
	 * sponsors.
	 * */
	public HomeAction(){
		  monBundle = (ImageBund)GWT.create(ImageBund.class);
		  action = new VerticalPanel();
		  text = new String();
		  icon= new Image();
		  icon=monBundle.addsponsor().createImage();
		  text=Joyeuxlurons.getHeaderString("Sponsor", icon);
		  ben = new Label("Benjamin RIAS");
		  tib= new Label("Thibault FAUTREL");
		  vide = new SimplePanel();
		  
		  delSponsor= new PushButton();
		  delSponsor.addClickHandler(new ClickHandler() {
			
			@Override
			public void onClick(ClickEvent event) {
				final PopupPanel popup = new PopupPanel();
				Label choix = new Label("Quel type de sponsor voulez-vous supprimer ?");
				final RadioButton radioArgent = new RadioButton("choix", "Argent");
				final RadioButton radioLot = new RadioButton("choix", "Lot");
				HorizontalPanel panelRadios = new HorizontalPanel();
				final ListBox liste = new ListBox();
				Button suppr = new Button("Supprimer", new ClickHandler() {

					@Override
					public void onClick(ClickEvent event) {
						AsyncCallback<Void> callback = new AsyncCallback<Void>() {

							@Override
							public void onFailure(Throwable caught) {
								final DialogBox msg = new DialogBox();
								VerticalPanel temp = new VerticalPanel();
								Label lb = new Label();
								lb.setText("Problème lors de la suppression du sponsor, il n'a pas pu être supprimé !");
								temp.add(lb);
								temp.add(new Button("OK", new ClickHandler() {
									
									@Override
									public void onClick(ClickEvent event) {
										msg.hide();
										popup.hide();
									}
								}));
								msg.add(temp);
								msg.setTitle("Oups!");
								msg.center();
							}

							@Override
							public void onSuccess(Void result) {
								final DialogBox msg = new DialogBox();
								VerticalPanel temp = new VerticalPanel();
								Label lb = new Label();
								lb.setText("Le sponsor a bien été supprimé !");
								temp.add(lb);
								temp.add(new Button("OK", new ClickHandler() {
									
									@Override
									public void onClick(ClickEvent event) {
										msg.hide();
										popup.hide();
									}
								}));
								msg.add(temp);
								msg.setTitle("Oups!");
								msg.center();
							}
						};
						if (liste.getItemCount()!=0 && radioArgent.getValue()) {
							services.delSponsorArgent(liste.getItemText(liste.getSelectedIndex()), callback);
						} else if (liste.getItemCount()!=0 && radioLot.getValue()) {
							services.delSponsorLot(liste.getItemText(liste.getSelectedIndex()), callback);
						} else {
							final DialogBox msg = new DialogBox();
							VerticalPanel temp = new VerticalPanel();
							Label lb = new Label();
							lb.setText("Veuillez sélectionner un type de sponsor, puis un sponsor à supprimer.");
							temp.add(lb);
							temp.add(new Button("OK", new ClickHandler() {
								
								@Override
								public void onClick(ClickEvent event) {
									msg.hide();
								}
							}));
							msg.add(temp);
							msg.setTitle("Oups!");
							msg.center();
						}
					}
				});
				Button cancel = new Button("Annuler", new ClickHandler() {

					@Override
					public void onClick(ClickEvent event) {
						popup.hide();
					}});
				HorizontalPanel panelBoutons = new HorizontalPanel();
				VerticalPanel panelSuppr = new VerticalPanel();
				
				panelBoutons.add(cancel);
				panelBoutons.add(suppr);
				
				final AsyncCallback<ArrayList<SponsorArgent>> callbackArgent = new AsyncCallback<ArrayList<SponsorArgent>>() {

					@Override
					public void onFailure(Throwable caught) {
						final DialogBox msg = new DialogBox();
						VerticalPanel temp = new VerticalPanel();
						Label lb = new Label();
						lb.setText("Problème lors de la récupération de la liste des sponsors (argent) depuis le serveur !");
						temp.add(lb);
						temp.add(new Button("OK", new ClickHandler() {
							
							@Override
							public void onClick(ClickEvent event) {
								msg.hide();
								popup.hide();
							}
						}));
						msg.add(temp);
						msg.setTitle("Oups!");
						msg.center();
					}

					@Override
					public void onSuccess(ArrayList<SponsorArgent> result) {
						liste.clear();
						for (SponsorArgent s : result) {
							liste.addItem(s.getNom());
						}
					}
					
				};
				
				final AsyncCallback<ArrayList<SponsorLot>> callbackLot = new AsyncCallback<ArrayList<SponsorLot>>() {

					@Override
					public void onFailure(Throwable caught) {
						final DialogBox msg = new DialogBox();
						VerticalPanel temp = new VerticalPanel();
						Label lb = new Label();
						lb.setText("Problème lors de la récupération de la liste des sponsors (argent) depuis le serveur !");
						temp.add(lb);
						temp.add(new Button("OK", new ClickHandler() {
							
							@Override
							public void onClick(ClickEvent event) {
								msg.hide();
								popup.hide();
							}
						}));
						msg.add(temp);
						msg.setTitle("Oups!");
						msg.center();
					}

					@Override
					public void onSuccess(ArrayList<SponsorLot> result) {
						liste.clear();
						for (SponsorLot s : result) {
							liste.addItem(s.getNom());
						}
					}
					
				};

				radioArgent.addClickHandler(new ClickHandler(){

					@Override
					public void onClick(ClickEvent event) {
						services.getListSponsorsArgent(callbackArgent);
					}});
				radioLot.addClickHandler(new ClickHandler(){

					@Override
					public void onClick(ClickEvent event) {
						services.getListSponsorsLot(callbackLot);
					}});
				panelRadios.add(radioArgent);
				panelRadios.add(radioLot);
				
				panelSuppr.add(choix);
				panelSuppr.add(panelRadios);
				panelSuppr.add(liste);
				panelSuppr.add(panelBoutons);
				
				popup.setTitle("Supprimer un sponsor");
				popup.add(panelSuppr);
				popup.center();
			}
		});
		  
		  nouveauSponsor= new PushButton("",new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				final PopupPanel popup = new PopupPanel();
				Label labelNom = new Label();
				Label labelAdresse = new Label();
				Label labelChoix = new Label();
				Label labelMontant = new Label();
				Label labelLot1 = new Label();
				Label labelLot2 = new Label();
				Label labelLot3 = new Label();
				final TextArea nom = new TextArea();
				final TextArea adresse = new TextArea();
				final TextArea montant = new TextArea();
				final TextArea lot1 = new TextArea();
				final TextArea lot2 = new TextArea();
				final TextArea lot3 = new TextArea();
				HorizontalPanel panelNom = new HorizontalPanel();
				HorizontalPanel panelAdresse = new HorizontalPanel();
				final HorizontalPanel panelArgent = new HorizontalPanel();
				final HorizontalPanel panelLot1 = new HorizontalPanel();
				final HorizontalPanel panelLot2 = new HorizontalPanel();
				final HorizontalPanel panelLot3 = new HorizontalPanel();
				final HorizontalPanel panelBas = new HorizontalPanel();
				final VerticalPanel panelChoix = new VerticalPanel();
				final VerticalPanel panelType = new VerticalPanel();
				final VerticalPanel panelPrinc = new VerticalPanel();
				HorizontalPanel panelRadioBoutons = new HorizontalPanel();
				final RadioButton choixLot = new RadioButton("choix", "Lot(s)");
				final RadioButton choixArgent = new RadioButton("choix", "Argent");
				/* WIDGETS POUR L'UPLOAD DE FICHIER*/
				final FormPanel uploadForm = new FormPanel();
				final FileUpload upload = new FileUpload();
				final HTML messageServeur = new HTML();

				
				labelNom.setText("Nom : ");
				nom.setHeight("22px");
				panelNom.add(labelNom);
				panelNom.add(nom);
				
				labelAdresse.setText("Adresse : ");
				panelAdresse.add(labelAdresse);
				panelAdresse.add(adresse);
				
				labelMontant.setText("Montant du don : ");
				montant.setHeight("22px");
				panelArgent.add(labelMontant);
				panelArgent.add(montant);
				
				labelLot1.setText("Lot 1 : ");
				lot1.setHeight("22px");
				panelLot1.add(labelLot1);
				panelLot1.add(lot1);
				labelLot2.setText("Lot 2 (optionnel) : ");
				lot2.setHeight("22px");
				panelLot2.add(labelLot2);
				panelLot2.add(lot2);
				labelLot3.setText("Lot 3 (optionnel) : ");
				lot3.setHeight("22px");
				panelLot3.add(labelLot3);
				panelLot3.add(lot3);
				
				labelChoix.setText("Choisissez le type de don le sponsor propose : ");
				choixArgent.addClickHandler(new ClickHandler() {
					
					@Override
					public void onClick(ClickEvent event) {
						panelType.clear();
						panelType.add(panelArgent);
					}
				});
				choixLot.addClickHandler(new ClickHandler() {
					
					@Override
					public void onClick(ClickEvent event) {
						panelType.clear();
						panelType.add(panelLot1);
						panelType.add(panelLot2);
						panelType.add(panelLot3);
					}
				});
				panelRadioBoutons.add(choixArgent);
				panelRadioBoutons.add(choixLot);
				panelChoix.add(panelRadioBoutons);
				
				panelPrinc.add(panelNom);
				panelPrinc.add(panelAdresse);
				panelPrinc.add(panelChoix);
				panelPrinc.add(panelType);
				
				// Associé un service au formulaire
				uploadForm.setAction(GWT.getModuleBaseURL() + "UploadFileServlet");
				
				// Pour ajouter un FileUpload widget, nous devons ajouter au formulaire
				// une méthode d'envoie POST, et un encodage multipart MIME.
				uploadForm.setEncoding(FormPanel.ENCODING_MULTIPART);
				uploadForm.setMethod(FormPanel.METHOD_POST);
				
				// Ajout du panel au formulaire
				uploadForm.setWidget(panelPrinc);
				
				// Propriétés du FileUpload widget.
				upload.setName("uploadFormElement");
				panelPrinc.add(upload);
				panelPrinc.add(messageServeur);
				
				final AsyncCallback<Void> callback = new AsyncCallback<Void>() {

					@Override
					public void onFailure(Throwable caught) {
						final DialogBox msg = new DialogBox();
						VerticalPanel temp = new VerticalPanel();
						Label lb = new Label();
						lb.setText("Problème lors de l'ajout du sponsor sur le serveur !");
						temp.add(lb);
						temp.add(new Button("OK", new ClickHandler() {
							
							@Override
							public void onClick(ClickEvent event) {
								msg.hide();
							}
						}));
						msg.add(temp);
						msg.setTitle("Oups!");
						msg.center();
					}

					@Override
					public void onSuccess(Void result) {
						final DialogBox msg = new DialogBox();
						VerticalPanel temp = new VerticalPanel();
						Label lb = new Label();
						lb.setText("Le nouveau sponsor a bien été ajouté sur le serveur !");
						temp.add(lb);
						temp.add(new Button("OK", new ClickHandler() {
							
							@Override
							public void onClick(ClickEvent event) {
								msg.hide();
							}
						}));
						msg.add(temp);
						msg.setTitle("Oups!");
						msg.center();
					}
				};
				
				// Ajout d'un bouton 'Submit'
				Button uploadSubmitButton = new Button("Ajouter le sponsor", new ClickHandler() {

					public void onClick(ClickEvent event) {
						if (nom.getText().isEmpty()) {
							final DialogBox msg = new DialogBox();
							VerticalPanel temp = new VerticalPanel();
							Label lb = new Label();
							lb.setText("Veuillez remplir le champ 'Nom' avant de valider.");
							temp.add(lb);
							temp.add(new Button("OK", new ClickHandler() {
								
								@Override
								public void onClick(ClickEvent event) {
									msg.hide();
								}
							}));
							msg.add(temp);
							msg.setTitle("Oups!");
							msg.center();
						} else if (adresse.getText().isEmpty()) {
							final DialogBox msg = new DialogBox();
							VerticalPanel temp = new VerticalPanel();
							Label lb = new Label();
							lb.setText("Veuillez remplir le champ 'Adresse' avant de valider.");
							temp.add(lb);
							temp.add(new Button("OK", new ClickHandler() {
								
								@Override
								public void onClick(ClickEvent event) {
									msg.hide();
								}
							}));
							msg.add(temp);
							msg.setTitle("Oups!");
							msg.center();
						} else if (!choixArgent.getValue() && !choixLot.getValue()) {
							final DialogBox msg = new DialogBox();
							VerticalPanel temp = new VerticalPanel();
							Label lb = new Label();
							lb.setText("Veuillez sélectionner le type de sponsor et renseigner les champs correspondants avant de valider.");
							temp.add(lb);
							temp.add(new Button("OK", new ClickHandler() {
								
								@Override
								public void onClick(ClickEvent event) {
									msg.hide();
								}
							}));
							msg.add(temp);
							msg.setTitle("Oups!");
							msg.center();
						} else if (choixArgent.getValue() && montant.getText().isEmpty()) {
							final DialogBox msg = new DialogBox();
							VerticalPanel temp = new VerticalPanel();
							Label lb = new Label();
							lb.setText("Veuillez remplir le champ 'Montant' avant de valider.");
							temp.add(lb);
							temp.add(new Button("OK", new ClickHandler() {
								
								@Override
								public void onClick(ClickEvent event) {
									msg.hide();
								}
							}));
							msg.add(temp);
							msg.setTitle("Oups!");
							msg.center();
						} else if (choixArgent.getValue()) {
							try {
								int tmp = Integer.parseInt(montant.getText());
								uploadForm.submit();
							} catch (NumberFormatException nfe) {
								final DialogBox msg = new DialogBox();
								VerticalPanel temp = new VerticalPanel();
								Label lb = new Label();
								lb.setText("Le champ 'Montant' doit être rempli avec un entier !");
								temp.add(lb);
								temp.add(new Button("OK", new ClickHandler() {
									
									@Override
									public void onClick(ClickEvent event) {
										msg.hide();
									}
								}));
								msg.add(temp);
								msg.setTitle("Oups!");
								msg.center();
							}
						} else if (choixLot.getValue() && lot1.getText().isEmpty()) {
							final DialogBox msg = new DialogBox();
							VerticalPanel temp = new VerticalPanel();
							Label lb = new Label();
							lb.setText("Veuillez remplir au moins le champ 'Lot 1' avant de valider.");
							temp.add(lb);
							temp.add(new Button("OK", new ClickHandler() {
								
								@Override
								public void onClick(ClickEvent event) {
									msg.hide();
								}
							}));
							msg.add(temp);
							msg.setTitle("Oups!");
							msg.center();
						} else if (upload.getFilename().isEmpty()) {
							final DialogBox msg = new DialogBox();
							VerticalPanel temp = new VerticalPanel();
							Label lb = new Label();
							lb.setText("Veuillez sélectionner une image avant de valider.");
							temp.add(lb);
							temp.add(new Button("OK", new ClickHandler() {
								
								@Override
								public void onClick(ClickEvent event) {
									msg.hide();
								}
							}));
							msg.add(temp);
							msg.setTitle("Oups!");
							msg.center();
						} else {
							uploadForm.submit();
						}
					}
				});
				
				Button cancel = new Button("Annuler", new ClickHandler() {
					
					@Override
					public void onClick(ClickEvent event) {
						popup.hide();
					}
				});
				
				panelBas.add(cancel);
				panelBas.add(uploadSubmitButton);
				
				panelPrinc.add(panelBas);
				
				uploadForm.addSubmitCompleteHandler(new SubmitCompleteHandler() {

					public void onSubmitComplete(SubmitCompleteEvent event) {
						messageServeur.setHTML(event.getResults());
						if (choixArgent.getValue()) {
							SponsorArgent s = new SponsorArgent();
							s.setSponsorArgent(nom.getText(), messageServeur.getText(), adresse.getText(), Integer.parseInt(montant.getText()));
							services.addSponsorArgent(s, callback);
						} else if (choixLot.getValue()) {
							SponsorLot s = new SponsorLot();
							ArrayList<String> lots = new ArrayList<String>();
							lots.add(lot1.getText());
							if (lot2.getText() != "") {
								lots.add(lot2.getText());
							}
							if (lot3.getText() != "") {
								lots.add(lot3.getText());
							}
							s.setSponsorLot(nom.getText(), messageServeur.getText(), adresse.getText(), lots);
							services.addSponsorLot(s, callback);
						}
						popup.hide();
					}
				});
				popup.setTitle("Ajouter un sponsor");
				popup.add(uploadForm);
				popup.center();
			}
		  });
		  
		  nouveauSponsor.setWidth("195px");
		  nouveauSponsor.setHTML(text);
		  
		  vide.setHeight("200px");
		  
		  
		  action.setSpacing(5);
		  icon=monBundle.supprSponsor().createImage();
		  text=Joyeuxlurons.getHeaderString("Suppression", icon);
		  delSponsor.setHTML(text);
		  action.add(nouveauSponsor);
		  action.add(delSponsor);
		  action.add(vide);
		  action.add(ben);
		  action.add(tib);
		  
		  
		  
		  
		  initWidget(action);
	}
	
}
